#include <stdio.h>
#include <string.h>
#include "file.h"

int main()
{
    int a;
    struct contt con;
    text_init(&con);
    do
    {
        printf("\n");
        printf("********************************\n");
        printf("******1 add       2 del    *****\n");
        printf("******3 change    4 see    *****\n");
        printf("******5 print     0 exit   *****\n");
        printf("***please choose you function***\n");
        printf("********************************\n");
        printf("\n");
        scanf("%d",&a);
        switch(a)
        {
            case 1:
            text_add(&con);
                break;
                case 2:
                text_del(&con);
                break;
                case 5:
                text_show(&con);
                break;
        }

    }while(a);
}


void text_init(struct contt* pc){
    memset(pc->data,0,sizeof(pc->data));
    pc->size=0;
}

int text_add(struct contt *pc)
{
    if(pc->size>=MAX)
    {
        printf("can not add:full");
        return -1;
    }

    printf("name:");
    scanf("%s",pc->data[pc->size].name);
    printf("age:");
    scanf("%d",&(pc->data[pc->size].age));
    printf("sex:");
    scanf("%s",pc->data[pc->size].sex);
    printf("tele:");
    scanf("%s",pc->data[pc->size].tele);

    pc->size++;
    printf("add success\n");
    return 0;
}

void text_show(const struct contt *pc)
{
    for(int i=0;i<pc->size;i++)
    {
        printf("[%d]%-10s\t%-6s\t%-5s\t%-15s\n",i+1,"name","age","sex","tele");
        printf("%-10s\t%-6d\t%-5s\t%-15s\n",pc->data[i].name,pc->data[i].age,pc->data[i].sex,pc->data[i].tele);
        printf("\n");
    }
}

void text_del(struct contt *pc)
{
    int pos;
    printf("please tell me delete pos:");
    scanf("%d",&pos);
    if(pos<0||pos>pc->size)
    {
        printf("delete failue\n");
        return ;
    }
    
    for(int i=pos-1;i<pc->size;i++)
    {
        pc->data[i]=pc->data[i+1];
    }
    pc->size--;
}